package com.puente.hook;

import com.liferay.portal.ModelListenerException;
import com.liferay.portal.ResourceActionsException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;

import com.liferay.portal.model.ModelListener;
import com.liferay.portal.model.Permission;
import com.liferay.portal.model.Resource;
import com.liferay.portal.model.ResourceAction;
import com.liferay.portal.model.ResourceConstants;
import com.liferay.portal.model.ResourcePermission;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.RoleConstants;
import com.liferay.portal.security.permission.ActionKeys;
import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.PermissionLocalServiceUtil;
import com.liferay.portal.service.ResourceActionLocalServiceUtil;
import com.liferay.portal.service.ResourceLocalServiceUtil;
import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
import com.liferay.portal.service.ResourcePermissionServiceUtil;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.service.persistence.ResourceActionUtil;
import com.liferay.portal.service.persistence.ResourcePermissionUtil;

import com.liferay.portlet.documentlibrary.model.DLFileEntry;
import com.liferay.portlet.documentlibrary.model.DLFolder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
//import com.liferay.portlet.documentlibrary.model.DLFileEntry;
/**
 *
 * @author james Stuart Milne & Joaquin Cabal
 * Updated by Meanson Wang / meansonw@gmail.com @ 20110429
 */
public class DlHookAction implements ModelListener {

    public void onAfterCreate(Object model) throws ModelListenerException {
        //Add your implementation here
        com.liferay.portlet.documentlibrary.model.DLFileEntry addedFileEntry
                = (com.liferay.portlet.documentlibrary.model.DLFileEntry) model;


    }

    public void onAfterAddAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
      
        //Add your implementation here
    }

    public void onAfterRemove(Object model) throws ModelListenerException {
        //Add your implementation here
    }

    public void onAfterRemoveAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
        //Add your implementation here
    }

    public void onAfterUpdate(Object model) throws ModelListenerException {
        //Add your implementation here
         com.liferay.portlet.documentlibrary.model.DLFileEntry addedFileEntry
                = (com.liferay.portlet.documentlibrary.model.DLFileEntry) model;
         
         // Updated by Meanson Wang for liferay 6.0.5
         DLFolder parentFolder = addedFileEntry.getFolder();
         if(parentFolder != null) {
             try {
                 Resource resource = ResourceLocalServiceUtil.getResource(parentFolder.getCompanyId(), DLFolder.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(parentFolder.getFolderId()));
                 List values = ResourcePermissionLocalServiceUtil.getResourcePermissions(parentFolder.getCompanyId(), DLFolder.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(resource.getPrimKey()));
                 Resource resourceFileEntry = ResourceLocalServiceUtil.getResource(addedFileEntry.getCompanyId(), DLFileEntry.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(addedFileEntry.getFileEntryId()));

                 // Remove default permissions
              	 Role guestRole =RoleLocalServiceUtil.getRole(parentFolder.getCompanyId(), "Guest");
              	 ResourcePermissionLocalServiceUtil.removeResourcePermissions(parentFolder.getCompanyId(), DLFileEntry.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, guestRole.getRoleId(), ActionKeys.VIEW);
              	 //Role commRole =RoleLocalServiceUtil.getRole(parentFolder.getCompanyId(), "Community Member");
             	 
                 
                 
                 for (int i=0;i<values.size();i++) {
                 	ResourcePermission resourcePermission=(ResourcePermission) values.get(i);
                 	Role role=RoleLocalServiceUtil.getRole(resourcePermission.getRoleId());

                 	String[] actionIds = new String[2] ;
                 	actionIds[0]=ActionKeys.ADD_DISCUSSION;
                 	actionIds[1]=ActionKeys.VIEW	;
 
                 	if (!role.getName().equals("Owner")) {
                 		ResourcePermissionServiceUtil.setIndividualResourcePermissions(addedFileEntry.getGroupId(), addedFileEntry.getCompanyId(), DLFileEntry.class.getName(), String.valueOf(addedFileEntry.getFileEntryId()),resourcePermission.getRoleId(), actionIds);
                 	//PermissionLocalServiceUtil.setRolePermissions(resourcePermission.getRoleId(), addedFileEntry.getCompanyId(), DLFileEntry.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(addedFileEntry.getFolderId()), actionIds);
                 	//PermissionLocalServiceUtil.setRolePermissions(resourcePermission.getRoleId(),actionIds, res.getResourceId());
                 	//PermissionLocalServiceUtil.setRolePermissions(p.getRoleId(), addedFolderEntry.getCompanyId(), DLFolder.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(addedFolderEntry.getFolderId()), actionIds);
                 	}
                 	
                 
                 }
                 
            	/*

                Resource resource = ResourceLocalServiceUtil.getResource(parentFolder.getCompanyId(), DLFolder.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(parentFolder.getFolderId()));
             // List<Role> roles = ResourceAction.getRoles(parentFolder.getCompanyId(), parentFolder.getGroupId(), resource);
              	Map values = RoleLocalServiceUtil.getResourceRoles(parentFolder.getCompanyId(), DLFolder.class.getName() , ResourceConstants.SCOPE_INDIVIDUAL,String.valueOf(resource.getPrimKey()));
              	
              	System.out.println(values.iterator());
               Resource resourceFileEntry = ResourceLocalServiceUtil.getResource(addedFileEntry.getCompanyId(), DLFileEntry.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(addedFileEntry.getFileEntryId()));

			for (Iterator iterator = values.iterator(); iterator.hasNext();) {
				Map.Entry value = (Map.Entry)iterator.next();
				List<String> actionIds = (List<String>) value.getValue();

				for (Iterator iterator2 = actionIds.iterator(); iterator2.hasNext();) {
					Object obj = iterator2.next();
					if(!"VIEW".equalsIgnoreCase((String)obj) && !"DELETE".equalsIgnoreCase((String)obj)) {
						iterator2.remove();
					}
				}
				String [] actions = new String[actionIds.size()];
				for (int i = 0; i < actionIds.size(); i++) {
					actions[i] = actionIds.get(i);
				}
				Role role = RoleLocalServiceUtil.getRole(addedFileEntry.getCompanyId(),(String) value.getKey());
				PermissionLocalServiceUtil.setRolePermissions(role.getRoleId(), actions, resourceFileEntry.getResourceId());
            	 
			}   */
            } catch (PortalException ex) {
                Logger.getLogger(DlHookAction.class.getName()).log(Level.SEVERE, null, ex);
                ex.printStackTrace();
            } catch (SystemException ex) {
                Logger.getLogger(DlHookAction.class.getName()).log(Level.SEVERE, null, ex);
                ex.printStackTrace();
            }

		}
    }

    public void onBeforeAddAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeCreate(Object model) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeRemove(Object model) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeRemoveAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeUpdate(Object model) throws ModelListenerException {
        //Add your implementation here

    }

}